<html><head>
      <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
   <title>Groovy</title><link rel="stylesheet" type="text/css" href="eclipse_book.css"><meta name="generator" content="DocBook XSL Stylesheets V1.77.1"><link rel="home" href="index.html" title="Red Hat Fuse Tooling"><link rel="up" href="RiderExLang.html" title="Part&nbsp;III.&nbsp;Expression and Predicates Languages"><link rel="prev" href="File.html" title="The File Language"><link rel="next" href="Header.html" title="Header"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="chapter"><div class="titlepage"><div><div><h2 class="title"><a name="Groovy"></a>Groovy</h2></div></div></div><div class="simplesect"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="topic-33199"></a>Overview</h2></div></div></div><p>Groovy is a Java-based scripting language that allows quick parsing of
object. The Groovy support is part of the <code class="literal">camel-groovy</code> module.</p></div><div class="simplesect"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="topic-33200"></a>Adding the script module</h2></div></div></div><p>To use Groovy in your routes you need to add a dependencies on
<code class="literal">camel-groovy</code> to your project as shown in
<a class="xref" href="Groovy.html#ExPredLangGroovyEx060" title="Example&nbsp;4.&nbsp;Adding the camel-groovy dependency">Example&nbsp;4, &#8220;Adding the camel-groovy dependency&#8221;</a>.</p><div class="example"><a name="ExPredLangGroovyEx060"></a><p class="title"><b>Example&nbsp;4.&nbsp;Adding the camel-groovy dependency</b></p><div class="example-contents"><pre class="screen">&lt;!-- Maven POM File --&gt;
&lt;properties&gt;
  &lt;camel-version&gt;2.21.0.fuse-730078-redhat-00001&lt;/camel-version&gt;
  ...
&lt;/properties&gt;

&lt;dependencies&gt;
  ...
  &lt;dependency&gt;
    &lt;groupId&gt;org.apache.camel&lt;/groupId&gt;
    &lt;artifactId&gt;camel-groovy&lt;/artifactId&gt;
    &lt;version&gt;${camel-version}&lt;/version&gt;
  &lt;/dependency&gt;
&lt;/dependencies&gt;</pre></div></div><br class="example-break"></div><div class="simplesect"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="topic-33201"></a>Static import</h2></div></div></div><p>To use the <code class="literal">groovy()</code> static method in your application code,
include the following import statement in your Java source files:</p><pre class="screen">import static org.apache.camel.builder.script.ScriptBuilder.*;</pre></div><div class="simplesect"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="topic-33202"></a>Built-in attributes</h2></div></div></div><p><a class="xref" href="Groovy.html#GroovyAttTbl010" title="Table&nbsp;7.&nbsp;Groovy attributes">Table&nbsp;7, &#8220;Groovy attributes&#8221;</a> lists the built-in attributes that are accessible
when using Groovy.</p><div class="table"><a name="GroovyAttTbl010"></a><p class="title"><b>Table&nbsp;7.&nbsp;Groovy attributes</b></p><div class="table-contents"><table summary="Groovy attributes" border="1"><colgroup><col width="33%" class="col_1"><col width="33%" class="col_2"><col width="34%" class="col_3"></colgroup><thead><tr><th align="left" valign="top">Attribute</th><th align="left" valign="top">Type</th><th align="left" valign="top">Value</th></tr></thead><tbody><tr><td align="left" valign="top"><p><code class="literal">context</code></p></td><td align="left" valign="top"><p><code class="literal">org.apache.camel.CamelContext</code></p></td><td align="left" valign="top"><p>The Camel Context</p></td></tr><tr><td align="left" valign="top"><p><code class="literal">exchange</code></p></td><td align="left" valign="top"><p><code class="literal">org.apache.camel.Exchange</code></p></td><td align="left" valign="top"><p>The current Exchange</p></td></tr><tr><td align="left" valign="top"><p><code class="literal">request</code></p></td><td align="left" valign="top"><p><code class="literal">org.apache.camel.Message</code></p></td><td align="left" valign="top"><p>The IN message</p></td></tr><tr><td align="left" valign="top"><p><code class="literal">response</code></p></td><td align="left" valign="top"><p><code class="literal">org.apache.camel.Message</code></p></td><td align="left" valign="top"><p>The OUT message</p></td></tr><tr><td align="left" valign="top"><p><code class="literal">properties</code></p></td><td align="left" valign="top"><p><code class="literal">org.apache.camel.builder.script.PropertiesFunction</code></p></td><td align="left" valign="top"><p>Function with a <code class="literal">resolve</code> method to make it easier to use the
              properties component inside scripts.</p></td></tr></tbody></table></div></div><br class="table-break"><p>The attributes all set at <code class="literal">ENGINE_SCOPE</code>.</p></div><div class="simplesect"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="topic-33203"></a>Example</h2></div></div></div><p><a class="xref" href="Groovy.html#ExPredLangGroovyEx090" title="Example&nbsp;5.&nbsp;Routes using Groovy">Example&nbsp;5, &#8220;Routes using Groovy&#8221;</a> shows two routes that use Groovy scripts.</p><div class="example"><a name="ExPredLangGroovyEx090"></a><p class="title"><b>Example&nbsp;5.&nbsp;Routes using Groovy</b></p><div class="example-contents"><pre class="screen">&lt;camelContext&gt;
  &lt;route&gt;
    &lt;from uri="direct:items" /&gt;
    &lt;filter&gt;
      &lt;language language="groovy"&gt;request.lineItems.any { i -&gt; i.value &gt; 100 }&lt;/language&gt;
      &lt;to uri="mock:mock1" /&gt;
    &lt;/filter&gt;
  &lt;/route&gt;
  &lt;route&gt;
    &lt;from uri="direct:in"/&gt;
    &lt;setHeader headerName="firstName"&gt;
      &lt;language language="groovy"&gt;$user.firstName $user.lastName&lt;/language&gt;
    &lt;/setHeader&gt;
    &lt;to uri="seda:users"/&gt;
  &lt;/route&gt;
&lt;/camelContext&gt;</pre></div></div><br class="example-break"></div><div class="simplesect"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="topic-33204"></a>Using the properties component</h2></div></div></div><p>To access a property value from the properties component, invoke the <code class="literal">resolve</code>
method on the built-in <code class="literal">properties</code> attribute, as follows:</p><pre class="screen">.setHeader("myHeader").groovy("properties.resolve(<span class="emphasis"><em>PropKey</em></span>)")</pre><p>Where <span class="emphasis"><em>PropKey</em></span> is the key of the property you want to resolve,
where the key value is of <code class="literal">String</code> type.</p><p>For more details about the properties component, see <span class="emphasis"><em><span class="citetitle"><a class="link" href="https://access.redhat.com/documentation/en-US/Red_Hat_JBoss_Fuse/6.2/html/Apache_Camel_Component_Reference/IDU-Properties.html" target="_top">Properties</a></span></em></span> in the <span class="emphasis"><em><span class="citetitle">Apache Camel Component Reference Guide</span></em></span>.</p></div><div class="simplesect"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_customizing_groovy_shell"></a>Customizing Groovy Shell</h2></div></div></div><p>Sometimes, you might need to use the custom <code class="literal">GroovyShell</code> instance, in your Groovy expressions.
To provide custom <code class="literal">GroovyShell</code>, add an implementation of the <code class="literal">org.apache.camel.language.groovy.GroovyShellFactory</code> SPI interface to your Camel registry.</p><p>For example, when you add the following bean to your Spring context, Apache Camel will use the custom <code class="literal">GroovyShell</code> instance that includes the custom static imports, instead of the default one.</p><pre class="screen">public class CustomGroovyShellFactory implements GroovyShellFactory {

  public GroovyShell createGroovyShell(Exchange exchange) {
    ImportCustomizer importCustomizer = new ImportCustomizer();
    importCustomizer.addStaticStars("com.example.Utils");
    CompilerConfiguration configuration = new CompilerConfiguration();
    configuration.addCompilationCustomizers(importCustomizer);
    return new GroovyShell(configuration);
  }
 }</pre></div></div></body></html>